package com.apes.fn.fico.expense.platformFee;

import com.alibaba.fastjson.JSONObject;
import com.apes.fn.fico.expense.ExpenseItemDetailsBill;
import com.apes.fn.fico.expense.ExpenseItemDetailsBillRepository;
import com.apes.framework.api.DomainService;
import com.apes.framework.util.Arith;
import com.apes.framework.util.MapUtil;
import com.apes.scm.masterdata.enumeration.repository.EnumerationRepository;
import com.apes.scm.masterdata.finance.repository.IncomeTypeRepository;
import com.apes.scm.masterdata.frame.repository.DeptRepository;
import com.apes.scm.masterdata.party.model.PartyRole;
import com.apes.scm.masterdata.party.repository.PartyRoleRepository;
import org.springframework.beans.factory.annotation.Autowired;

import java.util.Date;
import java.util.Map;

/**
 * 公共部分
 */
public abstract class CommonFeeProcess extends DomainService implements PlatformFeeProcess {

    @Autowired
    private IncomeTypeRepository incomeTypeRepository;
    @Autowired
    private EnumerationRepository enumerationRepository;
    @Autowired
    private ExpenseItemDetailsBillRepository expenseItemDetailsBillRepository;
    @Autowired
    private PartyRoleRepository partyRoleRepository;
    @Autowired
    private DeptRepository deptRepository;


    protected void createExpense(Map m) {
        Map map = getExpenseModel(m);
        if(map != null)
        // 创建费用单
        createExpenseByFee(map);
    }

    public abstract Map<String, Object> getExpenseModel(Map map);

    /**
     * 平台服务费生成平台费用细项单据
     *
     * @param map
     */
    protected void createExpenseByFee(Map map) {
        double expenseAmount = Double.parseDouble(map.get("expenseAmount").toString());
        if(expenseAmount == 0) return;
        ExpenseItemDetailsBill expenseItemDetailsBill = new ExpenseItemDetailsBill();
        //单据
        expenseItemDetailsBill.setOrderId(map.get("order").toString());
        //单据类型
        expenseItemDetailsBill.setOrderType(map.get("orderType").toString());
        //源单据
        expenseItemDetailsBill.setSource(map.get("source").toString());
        //源单据类型
        expenseItemDetailsBill.setSourceBusinessType(map.get("sourceBusinessType").toString());
        //摘要
        expenseItemDetailsBill.setSummary(map.get("summary").toString());
        //费用类型
        expenseItemDetailsBill.setIncomeType(incomeTypeRepository.findOne(map.get("incomeTypeId").toString()));
        //费用细项
        expenseItemDetailsBill.setExpenseDetailsType(enumerationRepository.findOne(map.get("expenseDetailsTypeId").toString()));
        //款项金额
        expenseItemDetailsBill.setPaymentAmount(Arith.round(Double.parseDouble(map.get("paymentAmount").toString()),2));
        //费用金额
        expenseItemDetailsBill.setExpenseAmount(Arith.round(expenseAmount,2));
        //公司
        expenseItemDetailsBill.setCompany(partyRoleRepository.findOne(map.get("companyId").toString()));
        //部门
        expenseItemDetailsBill.setDept(deptRepository.findOne(map.get("deptId").toString()));

        expenseItemDetailsBill.setStatisticDate((Date) map.get("date"));

        expenseItemDetailsBillRepository.saveAndFlush(expenseItemDetailsBill);
    }


    /**
     * 获取平台服务费率
     * 根据公司的运营模式、公司编码查询平台服务费率
     *
     * @param companyId 公司
     * @return 平台服务费率
     */
    protected double getPlatformFreeRatio(String companyId) {
        PartyRole company = partyRoleRepository.findOne(companyId);
        Map map = MapUtil.mapper(
                "OPERATION", company.getParty().getCorporate().getOperation().getId(),
                "COMPANY_ID", company.getId(), "SYS_RQ", new Date(), "conditionStrategyId", "FW_PT_SK"
        );
        Object obj = invoke("condition.execConditionStrategy", map);
        JSONObject result = new JSONObject(MapUtil.mapped(obj));
        return result.getDoubleValue("T001");
    }

}
